package com.sunspring.zdcall.service.impl;

import com.sunspring.zdcall.common.constant.enums.StateEnum;
import com.sunspring.zdcall.common.exception.ZdException;
import com.sunspring.zdcall.dto.ClientDto;
import com.sunspring.zdcall.entity.ZdBaseClient;
import com.sunspring.zdcall.entity.ZdClientLogin;
import com.sunspring.zdcall.mapper.master.ClientMapper;
import com.sunspring.zdcall.param.ClientParam;
import com.sunspring.zdcall.service.ClientService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;


/**
 * @Author XJ
 * @Date 2018/11/27  16:55
 */
@Service
@Slf4j
public class ClientServiceImpl implements ClientService {

    @Autowired
    private ClientMapper clientMapper;


    @Override
    public ClientDto getClientById(String clientId) {
        //查询参数信息
        ClientParam clientParam = new ClientParam();
        clientParam.setClientId(clientId);
        List<ClientDto> list = selectAll(clientParam);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }


    @Override
    public void register(ClientParam clientParam) {
        //判断客户端是注册还是更新
        if (clientMapper.selectCountByClientId(clientParam.getClientId()) == 0) {
            addNewClient(clientParam);
        }else {
            updateClient(clientParam);
        }
    }

    @Transactional(rollbackFor = ZdException.class)
    @Override
    public void addNewClient(ClientParam clientParam) {
        ZdBaseClient client = ZdBaseClient.clientFactory(clientParam.getClientId(),clientParam.getClientType());
        client.setClientName(clientParam.getClientName());
        int result = clientMapper.insertSelective(client);
        if (result <=0) {
            throw new ZdException("注册客户端失败");
        }
        String baseDeptId = clientParam.getBaseDeptId();
        //拆分科室id
        String[] checkroomIdLists = StringUtils.split(clientParam.getBaseCheckRoomId(),",");
        result = clientMapper.insertBatchCheckRoomMapping(clientParam.getClientId(),checkroomIdLists);
        if (result <=0) {
            throw new ZdException("注册客户端和检查室映射失败");
        }
        result = clientMapper.insertDeptMapping(clientParam.getClientId(),baseDeptId);
        if (result <=0) {
            throw new ZdException("注册客户端和科室映射失败");
        }
        log.info("客户端注册成功，客户端ID{}",clientParam.getClientId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateClient(ClientParam clientParam) {
        ZdBaseClient client = new ZdBaseClient();
        BeanUtils.copyProperties(clientParam,client);
        int result = clientMapper.updateSelective(client);
        if (result <= 0) {
            throw new ZdException("客户端信息保存失败");
        }
        String baseDeptId = clientParam.getBaseDeptId();
        //删除科室和客户端的映射,然后重写新增
        clientMapper.deleteCheckroomMappingByClientId(clientParam.getClientId());
        String[] checkroomIdLists = StringUtils.split(clientParam.getBaseCheckRoomId(),",");
        result = clientMapper.insertBatchCheckRoomMapping(clientParam.getClientId(),checkroomIdLists);
        if (result <=0) {
            throw new ZdException("保存客户端和检查室映射失败");
        }
        result = clientMapper.updateDeptMapping(clientParam.getClientId(),baseDeptId);
        if (result <=0) {
            throw new ZdException("保存客户端和科室映射失败");
        }
    }

    @Override
    public void saveProperties(ZdBaseClient client) {
        int result = clientMapper.updateSelective(client);
        if (result <= 0) {
            throw new ZdException("客户端信息保存失败");
        }
    }

    @Override
    public void switchClient(String clientId) {
        Integer state = clientMapper.selectStateByClientId(clientId);
        int result = clientMapper.updateStateByClientId(clientId,state == StateEnum.enable.getCode() ? StateEnum.disable.getCode():StateEnum.enable.getCode());
        if (result <=0) {
            throw new ZdException("修改客户端状态失败");
        }
        if (state == 1) {
            log.info("客户端" + clientId + "已被停用");
        }else if (state == 2) {
            log.info("客户端" + clientId + "已被启用");
        }else if (state == 3) {
            log.info("新注册的客户端" + clientId + "已被启用");
        }
    }

    @Override
    public void addClientLog(ZdClientLogin zdClientLogin) {
        if (clientMapper.insertClientLog(zdClientLogin) <= 0) {
            log.error("记录客户端日志失败");
        }
    }

    @Override
    public List<ClientDto> selectAll(ClientParam clientParam) {
        return clientMapper.selectAll(clientParam);
    }

    private static List<String> splitStr(String str) {
        String[] strs = StringUtils.split(str,",");
        List<String> list = new ArrayList<>();
        Collections.addAll(list, strs);
        return list;
    }

}
